package com.lg.atp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lg.atp.dao.DatasetDao;
import com.lg.atp.dao.ParaDao;
import com.lg.atp.dao.ParaDataDao;
import com.lg.atp.entity.TblDataset;
import com.lg.atp.entity.TblPara;
import com.lg.atp.entity.TblParaData;
import com.lg.atp.service.IDatasetService;

@Service("iDatasetService")
public class DatasetServiceImpl implements IDatasetService{

	@Autowired
	private DatasetDao datasetDao;
	@Autowired
	private ParaDao paraDao;
	@Autowired
	private ParaDataDao paraDataDao;
	
	@Override
	public List<TblDataset> selectAllDataset(TblDataset dataset) {
		return datasetDao.selectAllDataset(dataset);
	}

	@Override
	public List<TblDataset> selectDataset(TblDataset dataset) {
		return datasetDao.selectDataset(dataset);
	}

	@Override
	public TblDataset selectDatasetById(Long id) {
		return datasetDao.selectDatasetById(id);
	}

	
	@Transactional
	@Override
	public void saveDataset(TblDataset dataset) {
		Long datasetId = dataset.getId();
		if(datasetId != null) {
			paraDao.updateParaDatasetDisable(datasetId);
			datasetDao.updateDataset(dataset);
		}else
			datasetDao.saveDataset(dataset);
		
		datasetId = dataset.getId();
		List<TblPara> paraList = dataset.getParaList();
		for(TblPara para:paraList) {
			para.setDatasetId(datasetId);
			paraDao.savePara(para);
			Long paraId = para.getId();
			List<TblParaData> dataList = para.getParaDataList();
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("paraId", paraId);
			map.put("list", dataList);
			paraDataDao.saveParaDatas(map);
		}
		
	}

	@Override
	public PageInfo<TblDataset> getDatasetByPage(String param, Integer pageIndex, Integer pageSize) {
        
		PageHelper.startPage(pageIndex,pageSize);
		TblDataset dataset = new TblDataset();
		dataset.setDatasetName(param);
		List<TblDataset> list = datasetDao.selectAllDataset(dataset);
		PageInfo<TblDataset> page = new PageInfo<TblDataset>(list);
		return page;
	}


	@Override
	public Long hasSameName(Map<String, Object> map) {
		return datasetDao.hasSameName(map);
	}

	@Override
	public Long hasSameCode(Map<String, Object> map) {
		return datasetDao.hasSameCode(map);
	}

	@Override
	public TblDataset getDatasetWithPara(Long datasetId) {
		return datasetDao.getDatasetWithPara(datasetId);
	}

}
